package com.ruoyi.productCheck.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.productCheck.domain.CheckInformation;
import com.ruoyi.productCheck.service.CheckInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/checkInformation")
public class CheckInformationController extends BaseController {

    @Autowired
    CheckInformationService checkInformationService;



    /**
     * 保存成品检验记录
     */
    @Log(title = "保存成品检验记录", businessType = BusinessType.INSERT)
    @PostMapping("/addInformation")
    public AjaxResult addInformation(@RequestBody CheckInformation checkInformation) {
        checkInformation.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = checkInformationService.addInformation(checkInformation);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 查询成品检验记录
     */
    @GetMapping("/informationList")
    @ResponseBody
    public TableDataInfo informationList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                         @RequestParam(value = "endTime", required = false) String endTime) {
        CheckInformation checkInformation = new CheckInformation();
        checkInformation.setBeginTime(beginTime);
        checkInformation.setEndTime(endTime);
        startPage();
        checkInformation.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<CheckInformation> list = checkInformationService.informationList(checkInformation);
        return getDataTable(list);
    }

    @GetMapping("/createWord")
    public AjaxResult createWord(@RequestParam(value = "checkId", required = false) Long checkId) {
        String message = checkInformationService.createWord(checkId);
        if (message.equals("生成失败")) {
            return AjaxResult.error("生成Word文件失败");
        } else {
            return AjaxResult.success(message);
        }
    }

    /*
       查询已经做完的成品检验记录
*/
    @GetMapping("/selectInformation")
    public CheckInformation selectInformation(@RequestParam(value = "checkId", required = false) Long checkId) {
        CheckInformation checkInformation = checkInformationService.selectInformation(checkId);
        return checkInformation;
    }

    /**
     * 删除成品检验记录
     * *
     */
    @Log(title = "删除成品检验记录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{checkId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("checkId") Long checkId) {
        checkInformationService.remove(checkId);
        return AjaxResult.success("删除成功");
    }

    /*
      编辑成品检验记录
         */
    @PostMapping("/updateInformation")
    @ResponseBody
    public AjaxResult updateInformation(@RequestBody CheckInformation checkInformation) {
        int num = checkInformationService.updateInformation(checkInformation);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /*
    查询审核完的成品检验记录
      */
    @GetMapping("/auditInformation")
    public Map<String, Object> auditInformation() {
        Map<String, Object> resMap = new HashMap<>();
        List<CheckInformation> checkInformation = checkInformationService.auditInformation(SecurityUtils.getDeptId());
        resMap.put("mateCleanRecord", checkInformation);
        return resMap;
    }
}
